<template>
  <div>
    <Modal v-model="scoreModal"
           :title="saveActive === '考核自评' ? '自评' : '评价他人'"
           @on-ok="submitScore">
      <Input number
             v-model="score"
             :maxlength="10" />
    </Modal>
    <Modal v-model="examineModal"
           title="考核管理"
           width="600">
      <Tabs @on-click="changeExam">
        <TabPane label="我的评分"
                 name="我的评分">
          <Table :show-header="false"
                 :data="scoreData"
                 :columns="scoreColumns"></Table>
          <Page class="pull-right"
                :total="Page.total"
                :current="Page.current"
                :page-size="Page.pageSize"
                size="small"
                show-total
                @on-change="changePage"></Page>
        </TabPane>
        <TabPane label="考核自评"
                 name="考核自评">
          <Table :show-header="false"
                 :data="scoreData"
                 :columns="myscoreColumns"></Table>
          <Page class="pull-right"
                :total="Page.total"
                :current="Page.current"
                :page-size="Page.pageSize"
                size="small"
                show-total
                @on-change="changePage"></Page>
        </TabPane>
        <TabPane label="评价他人"
                 name="评价他人">
          <Table :show-header="false"
                 :data="scoreData"
                 :columns="myscoreColumns"></Table>
          <Page class="pull-right"
                :total="Page.total"
                :current="Page.current"
                :page-size="Page.pageSize"
                size="small"
                show-total
                @on-change="changePage"></Page>
        </TabPane>
      </Tabs>
    </Modal>
  </div>
</template>

<script>
import { ajaxPost } from 'global/axios';
import storage from 'good-storage';
export default {
  data() {
    return {
      urlMyScoreList: '/demo/bus/assess/pcmyassess',
      urlAssessMy: '/demo/bus/assess/pclistmy',
      urlAssessOther: '/demo/bus/assess/pclistuser',
      urlAssess: '/demo/bus/assess/assess',
      examineModal: false,
      scoreData: [],
      scoreColumns: [
        {
          title: '时间',
          key: '_id',
        },
        {
          title: '评分',
          key: 'score',
          render: (h, params) => {
            if (params.row.score) {
              return h('div', params.row.score);
            } else {
              return h('div', '暂无评分');
            }
          },
        },
      ],
      saveActive: '',
      myscoreColumns: [
        {
          title: '时间',
          key: 'month',
        },
        {
          title: '',
          render: (h, params) => {
            if (this.saveActive === '评价他人') {
              return h('div', params.row.realname);
            }
          },
        },
        {
          title: '评分',
          render: (h, params) => {
            return h(
              'Button',
              {
                props: {
                  type: 'primary',
                },
                on: {
                  click: () => {
                    this.scoreModal = true;
                    this.score = '';
                    this.saveRow = params.row;
                  },
                },
              },
              this.saveActive === '考核自评' ? '自评' : '评价'
            );
          },
        },
      ],
      Page: {
        current: 1,
        pageSize: 10,
        total: 0,
        Page: 1,
      },
      scoreModal: false,
      score: '',
    };
  },
  methods: {
    changeExam(data) {
      this.Page.page = 1;
      this.saveActive = data;
      this.getAssessList();
    },
    changePage(data) {
      this.Page.page = data;
      this.getAssessList();
    },
    getAssessList() {
      let params = {
        data: {
          page: this.Page.page,
          pageSize: this.Page.pageSize,
        },
        url: this.urlMyScoreList,
        token: storage.session.get('token'),
        success: res => {
          this.$Loading.finish();
          if (res.data.code === '0000') {
            this.scoreData = res.data.result.result;
            this.Page.total = res.data.result.totalSize;
          }
        },
      };
      if (this.saveActive === '考核自评') {
        params.url = this.urlAssessMy;
      } else if (this.saveActive === '评价他人') {
        params.url = this.urlAssessOther;
      }
      ajaxPost(params);
    },
    submitScore() {
      let params = {
        data: {
          score: this.score,
          userid: this.saveRow.userid,
          realname: this.saveRow.realname,
          month: this.saveRow.month,
        },
        url: this.urlAssess,
        token: storage.session.get('token'),
        success: res => {
          if (res.data.code === '0000') {
            this.$Loading.finish();
            this.$Message.success('评价成功');
            this.getAssessList();
          } else {
            this.$Message.warning('操作失败');
          }
        },
      };
      ajaxPost(params);
    },
    show() {
      this.examineModal = true;
    },
  },
  mounted() {
    this.getAssessList();
  },
};
</script>
